package com.javasm.finance.controller;

import com.javasm.finance.entity.CodeAndMsg;
import com.javasm.finance.entity.Menu;
import com.javasm.finance.entity.PageInfo;
import com.javasm.finance.entity.ReturnEntity;
import com.javasm.finance.service.MenuService;
import com.javasm.finance.service.impl.MenuServiceImpl;
import com.javasm.finance.util.RespUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/menu/*")
public class MenuController extends BaseController {
    MenuService menuService=new MenuServiceImpl();

    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String menuName=req.getParameter("menuName");
        String parentIdStr = req.getParameter("parentId");
        Integer parentId = null;
        if (parentIdStr != null && !"".equals(parentIdStr)) {
            parentId = Integer.valueOf(parentIdStr);
        }
        Integer page=1;
        String pageStr=req.getParameter("page");
        if (pageStr!=null&&!"".equals(pageStr)){
            page=Integer.valueOf(pageStr);
        }
        Integer pageSize=5;
        String pageSizeStr=req.getParameter("pageSize");
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize=Integer.valueOf(pageSizeStr);
        }
        List<Menu> menuList = menuService.findAllMenu(page,pageSize,menuName,parentId);
        ReturnEntity re=new ReturnEntity();
        if (menuList!=null&&menuList.size()>0){
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            Map<String,Object> map=new HashMap<>();
            map.put("menuList",menuList);
            long totalNum = menuService.findTotalNum(menuName,parentId);
            PageInfo pageInfo=new PageInfo(page,pageSize,totalNum);
            map.put("pageInfo",pageInfo);
            re.setReturnData(map);
        }else {
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
            Map<String,Object> map=new HashMap<>();
            PageInfo pageInfo=new PageInfo(1,5,0l);
            map.put("pageInfo",pageInfo);
            re.setReturnData(map);
        }
        RespUtils.handleResp(resp,re);
    }

    public void queryOneMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MenuService menuService=new MenuServiceImpl();
        List<Menu> allOneMenuList = menuService.findAllOneMenu();
        ReturnEntity re=new ReturnEntity();
        if (allOneMenuList!=null&&allOneMenuList.size()>0){
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(allOneMenuList);
        }else {
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
        }
        RespUtils.handleResp(resp,re);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String menuIdStr = req.getParameter("menuId");
        Integer menuId = null;
        if (menuIdStr != null && !"".equals(menuIdStr)) {
            menuId = Integer.valueOf(menuIdStr);
        }
        String menuName = req.getParameter("menuName");
        String parentIdStr = req.getParameter("parentId");
        Integer parentId = null;
        if (parentIdStr != null && !"".equals(parentIdStr)) {
            parentId = Integer.valueOf(parentIdStr);
        }
        String menuUrl = req.getParameter("menuUrl");
        String glyphicon = req.getParameter("glyphicon");
        Menu menu = new Menu(menuId, menuName, parentId, menuUrl, glyphicon, 100);
        int rows = menuService.addMenu(menu);
        ReturnEntity re=new ReturnEntity();
        if (rows>0){
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        }else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        RespUtils.handleResp(resp,re);

    }

    public void queryByMenuId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String menuIdStr = req.getParameter("menuId");
        Integer menuId = null;
        if (menuIdStr != null && !"".equals(menuIdStr)) {
            menuId = Integer.valueOf(menuIdStr);
        }
        //调用业务层方法
        Menu menu = menuService.queryByMenuId(menuId);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (menu != null) {
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(menu);
        } else {
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
        }
        RespUtils.handleResp(resp,re);
    }

    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String menuIdStr = req.getParameter("menuId");
        Integer menuId = null;
        if (menuIdStr != null && !"".equals(menuIdStr)) {
            menuId = Integer.valueOf(menuIdStr);
        }
        String menuName = req.getParameter("menuName");
        String parentIdStr = req.getParameter("parentId");
        Integer parentId = null;
        if (parentIdStr != null && !"".equals(parentIdStr)) {
            parentId = Integer.valueOf(parentIdStr);
        }
        String menuUrl = req.getParameter("menuUrl");
        String glyphicon = req.getParameter("glyphicon");
        String versionIdStr = req.getParameter("versionId");
        Integer versionId = null;
        if (versionIdStr != null && !"".equals(versionIdStr)) {
            versionId = Integer.valueOf(versionIdStr);
        }
        Menu menu = new Menu(menuId, menuName, parentId, menuUrl, glyphicon, versionId);
        //调用业务层方法
        int rows = menuService.editMenu(menu);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (rows > 0) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String menuIdStr = req.getParameter("menuId");
        Integer menuId = null;
        if (menuIdStr != null && !"".equals(menuIdStr)) {
            menuId = Integer.valueOf(menuIdStr);
        }
        //调用业务层方法
        boolean isSuccess = menuService.deleteMenu(menuId);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (isSuccess) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }
}
